unit App.Imp.TAppConfigurationLoaderImp;

interface

uses SmallStruct.BusinessObject.TSSAdvancedBusinessObject,
  SmallStruct.Core,
  SmallStruct.Configuration;

type
  TAppConfigurationLoaderImp = class(TSSAdvancedBusinessObject, ISSConfigurationLoader)
  private
    function LoadActionConfiguration(const AActionName: string): ISSConfiguration;
    function LoadActionViewConfiguration(const AActionViewName: string): ISSConfiguration;
    function LoadBusinessObjectConfiguration(const ABusinessObjectName: string): ISSConfiguration;
    function LoadDatabaseConnectionConfiguration: ISSObjectContainer; overload;
    function LoadDatabaseConnectionConfiguration(const ADatabaseConnectionName: string): ISSConfiguration; overload;
    function LoadModelConfiguration(const AModelName: string): ISSConfiguration;
    function LoadViewConfiguration(const AViewName: string): ISSConfiguration;
    function LoadViewExtendConfiguration(const AViewName, AViewExtendName: string): ISSConfiguration;
  end;

implementation

uses
  SysUtils,
  Classes,
  SmallStruct.Database,
  SmallStruct.Error,
  dialogs,
  SmallStruct.Configuration.TSSModelConfiguration,
  SmallStruct.Configuration.TSSViewConfiguration,
  SmallStruct.Configuration.TSSDatabaseConnectionConfiguration,
  SmallStruct.Configuration.TSSBusinessObjectConfiguration,
  SmallStruct.Configuration.TSSActionConfiguration,
  SmallStruct.Configuration.TSSActionViewConfiguration,
  SmallStruct.Configuration.TSSViewExtendConfiguration,
  SmallStruct.Core.TSSObjectContainer;

const
  S_ACTION_BO_TABLE = 'S5_ACTION_BO_V';
  S_ACTION_TABLE = 'S5_ACTION';
  S_ACTIONVIEW_ACTION_TABLE = 'S5_ACTIONVIEW_ACTION_V';
  S_ACTIONVIEW_TABLE = 'S5_ACTIONVIEW_V';
  S_BO_BO_TABLE = 'S5_BO_BO_V';
  S_BO_TABLE = 'S5_BO_V';
  S_DB_CONNECTION_TABLE = 'S5_DB_CONNECTION';
  S_MODEL_FIELD_TABLE = 'S5_MODEL_FIELD';
  S_MODEL_TABLE = 'S5_MODEL_V';
  S_VIEW_FIELD_TABLE = 'S5_VIEW_FIELD_V';
  S_VIEW_MODEL_TABLE = 'S5_VIEW_MODEL_V';
  S_VIEW_TABLE = 'S5_VIEW_V';
  S_VIEW_VIEW_TABLE = 'S5_VIEW_VIEW_V';
  S_VIEW_EXTEND_TABLE = 'S5_VIEW_EXTEND_V';
  S_VIEW_EXTEND_FIELD_TABLE = 'S5_VIEW_EXTEND_FIELD_V';
  S_ACTION_FILTER_TABLE = 'S5_ACTION_FILTER_V';

  { TAppConfigurationLoaderImp }

function TAppConfigurationLoaderImp.LoadActionConfiguration(const AActionName: string): ISSConfiguration;
var
  ActionID: Integer;
  Q: ISSDataSet;
  ActionConfiguration: TSSActionConfiguration;
  I: Integer;
begin
  Q := Query('SELECT * FROM ' + S_ACTION_TABLE + ' WHERE ACTION_NAME = ''' + AActionName + '''');

  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(AActionName);

    ActionConfiguration := TSSActionConfiguration.Create(nil);
    Result := ActionConfiguration;

    ActionID := Q.GetDataSet.FieldByName('ACTION_ID').AsInteger;

    Q.ReleaseObjectInstance;
    Q := nil;

    Q := Query('SELECT * FROM ' + S_ACTION_BO_TABLE + ' WHERE ACTION_ID = ' + IntToStr(ActionID));

    while not Q.GetDataSet.Eof do
    begin
      ActionConfiguration.GetBusinessObjectName.Add(Q.GetDataSet.FieldByName('BO_NAME').AsString);

      Q.GetDataSet.Next;
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadActionViewConfiguration(const AActionViewName: string): ISSConfiguration;
var
  ActionViewID: Integer;
  Q: ISSDataSet;
  FilterQuery: ISSDataSet;
  ActionViewConfiguration: TSSActionViewConfiguration;
  ActionConfiguration: TSSActionViewActionConfiguration;
begin
  Q := Query('SELECT * FROM ' + S_ACTIONVIEW_TABLE + ' WHERE ACTIONVIEW_NAME = ''' + AActionViewName + '''');

  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(AActionViewName);

    ActionViewConfiguration := TSSActionViewConfiguration.Create(nil);

    ActionViewConfiguration.SetActionViewName(AActionViewName);
    ActionViewConfiguration.SetActionViewCaption(Q.GetDataSet.FieldByName('ACTIONVIEW_CAPTION').AsString);
    ActionViewConfiguration.SetType(TSSActionViewType(Q.GetDataSet.FieldByName('ACTIONVIEW_TYPE_ID').AsInteger));
    ActionViewConfiguration.SetViewName(Q.GetDataSet.FieldByName('VIEW_NAME').AsString);

    Result := ActionViewConfiguration;

    ActionViewID := Q.GetDataSet.FieldByName('ACTIONVIEW_ID').AsInteger;

    Q.ReleaseObjectInstance;
    Q := nil;

    Q := Query('SELECT * FROM ' + S_ACTIONVIEW_ACTION_TABLE + ' WHERE ACTIONVIEW_ID = ' + IntToStr(ActionViewID) + ' ORDER BY  DISPLAY_ORDER');

    while not Q.GetDataSet.Eof do
    begin
      ActionConfiguration := TSSActionViewActionConfiguration.Create(nil);

      ActionConfiguration.SetActionName(Q.GetDataSet.FieldByName('REQUEST_ACTION_NAME').AsString);
      ActionConfiguration.SetRequestActionViewName(Q.GetDataSet.FieldByName('REQUEST_ACTIONVIEW_NAME').AsString);
      ActionConfiguration.SetAutoClose(Q.GetDataSet.FieldByName('IS_AUTO_CLOSE').AsInteger <> 0);
      ActionConfiguration.SetDisplay(Q.GetDataSet.FieldByName('IS_DISPLAY').AsInteger <> 0);
      ActionConfiguration.SetActionCatalog(Q.GetDataSet.FieldByName('CATALOG_NAME').AsString);
      ActionConfiguration.SetActionCaption(Q.GetDataSet.FieldByName('ACTION_CAPTION').AsString);

      try
        FilterQuery := Query('SELECT * FROM ' + S_ACTION_FILTER_TABLE + ' WHERE ACTION_NAME = ''' + ActionConfiguration.GetActionName + ''' ORDER BY FILTER_ORDER');

        while not FilterQuery.GetDataSet.Eof do
        begin
          ActionConfiguration.GetActionFilterName.Add(FilterQuery.GetStringValue('BO_NAME'));
        end;
      finally
        FilterQuery.ReleaseObjectInstance;
        FilterQuery := nil;
      end;

      ActionViewConfiguration.GetActionConfiguration.AppendObject(ActionConfiguration.GetActionCaption, ActionConfiguration);

      Q.GetDataSet.Next;
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadBusinessObjectConfiguration(const ABusinessObjectName: string): ISSConfiguration;
var
  Q: ISSDataSet;
  BOID: Integer;
  BOConfiguration: TSSBusinessObjectConfiguration;
begin
  Q := Query('SELECT * FROM ' + S_BO_TABLE + ' WHERE BO_NAME = ''' + ABusinessObjectName + '''');
  //showmessage(ABusinessObjectName);

  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(ABusinessObjectName);

    BOConfiguration := TSSBusinessObjectConfiguration.Create(nil);
    BOConfiguration.SetBusinessObjectName(ABusinessObjectName);
    BOConfiguration.SetDatabaseConnectionName(Q.GetDataSet.FieldByName('DB_CONNECTION_NAME').AsString);
    BOConfiguration.SetRequireDatabase(Q.GetDataSet.FieldByName('IS_REQUIRE_DATABASE').AsInteger > 0);
    BOConfiguration.SetBaseScript(Q.GetDataSet.FieldByName('IS_BASE_SCRIPT').AsInteger > 0);

    Result := BOConfiguration;

    BOID := Q.GetDataSet.FieldByName('BO_ID').AsInteger;

    Q.ReleaseObjectInstance;
    Q := nil;

    Q := Query('SELECT * FROM ' + S_BO_BO_TABLE + ' WHERE BO_ID = ' + IntToStr(BOID));

    while not Q.GetDataSet.Eof do
    begin
      BOConfiguration.GetReferenceBusinessObjectName.Add(Q.GetDataSet.FieldByName('REF_BO_NAME').AsString);

      Q.GetDataSet.Next;
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadDatabaseConnectionConfiguration: ISSObjectContainer;
var
  Q: ISSDataSet;
  DatabaseConnectionConfiguration: TSSDatabaseConnectionConfiguration;
begin
  Result := TSSObjectContainer.Create(nil);

  Q := Query('SELECT * FROM ' + S_DB_CONNECTION_TABLE);

  try
    while Q.GetDataSet.Eof do
    begin
      DatabaseConnectionConfiguration := TSSDatabaseConnectionConfiguration.Create(nil);

      DatabaseConnectionConfiguration.SetConnectionName(Q.GetDataSet.FieldByName('DB_CONNECTION_NAME').AsString);
      DatabaseConnectionConfiguration.SetConnectionString(Q.GetDataSet.FieldByName('CONNECTION_STRING').AsString);
      DatabaseConnectionConfiguration.SetDriverClassName(Q.GetDataSet.FieldByName('DRIVER_CLASS_NAME').AsString);
      DatabaseConnectionConfiguration.SetConnectionName(Q.GetDataSet.FieldByName('DB_CONNECTION_NAME').AsString);

      Result.AppendObject(DatabaseConnectionConfiguration.GetConnectionName, DatabaseConnectionConfiguration);
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadModelConfiguration(const AModelName: string): ISSConfiguration;
var
  Q: ISSDataSet;
  ModelConfiguration: TSSModelConfiguration;
  FieldConfiguration: TSSModelFieldConfiguration;
  ModelID: Integer;
begin
  Q := Query('SELECT * FROM ' + S_MODEL_TABLE + ' WHERE MODEL_NAME = ''' + AModelName + '''');

  //SHOWMESSAGE(AModelName);
  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(AModelName);

    ModelConfiguration := TSSModelConfiguration.Create(nil);
    ModelConfiguration.SetDatabaseConnectionName(Q.GetDataSet.FieldByName('DB_CONNECTION_NAME').AsString);
    ModelConfiguration.SetEntityName(Q.GetDataSet.FieldByName('ENTITY_NAME').AsString);
    ModelConfiguration.SetOpenCondition(Q.GetDataSet.FieldByName('OPEN_CONDITION').AsString);
    ModelConfiguration.SetAutoOpen(Q.GetDataSet.FieldByName('IS_AUTO_OPEN').AsInteger > 0);
    ModelConfiguration.SetReadOnly(Q.GetDataSet.FieldByName('IS_READ_ONLY').AsInteger > 0);
    ModelConfiguration.SetType(TSSModelType(Q.GetDataSet.FieldByName('MODEL_TYPE_ID').AsInteger));
    ModelConfiguration.SetModelName(Q.GetDataSet.FieldByName('MODEL_NAME').AsString);

    Result := ModelConfiguration;

    ModelID := Q.GetDataSet.FieldByName('MODEL_ID').AsInteger;

    Q.ReleaseObjectInstance;
    Q := nil;

    Q := Query('SELECT * FROM ' + S_MODEL_FIELD_TABLE + ' WHERE MODEL_ID = ' + IntToStr(ModelID));

    while not Q.GetDataSet.Eof do
    begin
      FieldConfiguration := TSSModelFieldConfiguration.Create(nil);
      FieldConfiguration.SetFieldCaption(Q.GetDataSet.FieldByName('FIELD_CAPTION').AsString);
      FieldConfiguration.SetFieldName(Q.GetDataSet.FieldByName('FIELD_NAME').AsString);
      FieldConfiguration.SetPrimaryKey(Q.GetDataSet.FieldByName('IS_PK').AsInteger > 0);
      FieldConfiguration.SetReadOnly(Q.GetDataSet.FieldByName('IS_READ_ONLY').AsInteger > 0);
      FieldConfiguration.SetRequired(Q.GetDataSet.FieldByName('IS_REQUIRED').AsInteger > 0);

      ModelConfiguration.GetFieldConfiguration.AppendObject(FieldConfiguration.GetFieldName, FieldConfiguration);

      Q.GetDataSet.Next;
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadViewConfiguration(const AViewName: string): ISSConfiguration;
var
  Q: ISSDataSet;
  ViewConfiguration: TSSViewConfiguration;
  ViewID: Integer;
  ViewFieldConfiguration: TSSViewFieldConfiguration;
begin
  Q := Query('SELECT * FROM ' + S_VIEW_TABLE + ' WHERE VIEW_NAME = ''' + AViewName + '''');

  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(AViewName);

    ViewConfiguration := TSSViewConfiguration.Create(nil);
    ViewConfiguration.SetViewName(Q.GetDataSet.FieldByName('VIEW_NAME').AsString);
    ViewConfiguration.SetPrimaryModelName(Q.GetDataSet.FieldByName('MODEL_NAME').AsString);
    ViewConfiguration.SetType(TSSViewType(Q.GetDataSet.FieldByName('VIEW_TYPE_ID').AsInteger));
    ViewConfiguration.SetMultiDataChoose(Q.GetDataSet.FieldByName('IS_MULTI_DATA_CHOOSE').AsInteger > 0);
    ViewConfiguration.SetReadOnly(Q.GetDataSet.FieldByName('IS_READ_ONLY').AsInteger > 0);

    Result := ViewConfiguration;

    ViewID := Q.GetDataSet.FieldByName('VIEW_ID').AsInteger;

    Q.ReleaseObjectInstance;
    Q := nil;

    Q := Query('SELECT * FROM ' + S_VIEW_MODEL_TABLE + ' WHERE VIEW_ID = ' + IntToStr(ViewID));

    while not Q.GetDataSet.Eof do
    begin
      ViewConfiguration.GetModelName.Add(Q.GetDataSet.FieldByName('MODEL_NAME').AsString);

      Q.GetDataSet.Next;
    end;

    Q.ReleaseObjectInstance;
    Q := nil;

    Q := Query('SELECT * FROM ' + S_VIEW_FIELD_TABLE + ' WHERE VIEW_ID = ' + IntToStr(ViewID) + ' ORDER BY DISPLAY_ORDER');

    while not Q.GetDataSet.Eof do
    begin
      ViewFieldConfiguration := TSSViewFieldConfiguration.Create(nil);

      ViewFieldConfiguration.SetFieldName(Q.GetDataSet.FieldByName('FIELD_NAME').AsString);
      ViewFieldConfiguration.SetCol(Q.GetDataSet.FieldByName('DISPLAY_COL').AsInteger);
      ViewFieldConfiguration.SetHeight(Q.GetDataSet.FieldByName('DISPLAY_HEIGHT').AsInteger);
      ViewFieldConfiguration.SetLinkFieldName(Q.GetDataSet.FieldByName('LINK_FIELD_NAME').AsString);
      ViewFieldConfiguration.SetLinkModelName(Q.GetDataSet.FieldByName('LINK_MODEL_NAME').AsString);
      ViewFieldConfiguration.SetLinkKeyFieldName(Q.GetDataSet.FieldByName('LINK_KEY_FIELD_NAME').AsString);
      ViewFieldConfiguration.SetRow(Q.GetDataSet.FieldByName('DISPLAY_ROW').AsInteger);
      ViewFieldConfiguration.SetType(TSSViewFieldType(Q.GetDataSet.FieldByName('VIEW_FIELD_TYPE_ID').AsInteger));
      ViewFieldConfiguration.SetWidth(Q.GetDataSet.FieldByName('DISPLAY_WIDTH').AsInteger);
      ViewFieldConfiguration.SetVisiable(Q.GetDataSet.FieldByName('IS_VISIABLE').AsInteger = 1);

      ViewConfiguration.GetFieldConfiguration.AppendObject(ViewFieldConfiguration.GetFieldName, ViewFieldConfiguration);
      Q.GetDataSet.Next;
    end;

    if ViewConfiguration.GetType = ssvtMultiView then
    begin
      Q.ReleaseObjectInstance;
      Q := nil;

      Q := Query('SELECT * FROM ' + S_VIEW_VIEW_TABLE + ' WHERE VIEW_ID = ' + IntToStr(ViewID));

      while not Q.GetDataSet.Eof do
      begin
        ViewConfiguration.GetInnerViewName.Add(Q.GetDataSet.FieldByName('INNER_VIEW_NAME').AsString);
        Q.GetDataSet.Next;
      end;
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadViewExtendConfiguration(const AViewName, AViewExtendName: string): ISSConfiguration;
var
  Q: ISSDataSet;
  ViewExtendConfiguration: TSSViewExtendConfiguration;
  ViewExtendFieldConfiguration: TSSViewExtendFieldConfiguration;
begin
  Q := Query('SELECT * FROM ' + S_VIEW_EXTEND_FIELD_TABLE + ' WHERE VIEW_NAME = ''' + AViewName + ''' AND VIEW_EXTEND_CAPTION = ''' + AViewExtendName + ''' ORDER BY DISPLAY_ORDER');

  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(AViewName + '.' + AViewExtendName);

    ViewExtendConfiguration := TSSViewExtendConfiguration.Create(nil);

    while not Q.GetDataSet.Eof do
    begin
      ViewExtendFieldConfiguration := TSSViewExtendFieldConfiguration.Create(nil);
      ViewExtendFieldConfiguration.SetFieldName(Q.GetDataSet.FieldByName('FIELD_NAME').AsString);
      ViewExtendFieldConfiguration.SetAggregateGroup(Q.GetDataSet.FieldByName('IS_AGGREGATE_GROUP').AsInteger = 1);
      ViewExtendFieldConfiguration.SetAggregateType(TSSAggregateType(Q.GetDataSet.FieldByName('AGGREGATE_TYPE_ID').AsInteger));
      ViewExtendFieldConfiguration.SetDisplayOrder(Q.GetDataSet.FieldByName('DISPLAY_ORDER').AsInteger);
      ViewExtendFieldConfiguration.SetGroupOrder(Q.GetDataSet.FieldByName('GROUP_ORDER').AsInteger);
      ViewExtendFieldConfiguration.SetSortOrder(TSSSortOrder(Q.GetDataSet.FieldByName('SORT_ORDER_ID').AsInteger));
      ViewExtendFieldConfiguration.SetDisplay(Q.GetDataSet.FieldByName('IS_DISPLAY').AsInteger = 1);
      ViewExtendFieldConfiguration.SetGroup(Q.GetDataSet.FieldByName('IS_GROUP').AsInteger = 1);

      ViewExtendConfiguration.GetFieldConfiguration.AppendObject(ViewExtendFieldConfiguration.GetFieldName, ViewExtendFieldConfiguration);
      Q.GetDataSet.Next;
    end;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

function TAppConfigurationLoaderImp.LoadDatabaseConnectionConfiguration(const ADatabaseConnectionName: string): ISSConfiguration;
var
  Q: ISSDataSet;
  DatabaseConnectionConfiguration: TSSDatabaseConnectionConfiguration;
begin
  Q := Query('SELECT * FROM ' + S_DB_CONNECTION_TABLE + ' WHERE DB_CONNECTION_NAME = ''' + ADatabaseConnectionName + '''');

  try
    if Q.GetDataSet.RecordCount <> 1 then
      raise ESSConfigurationNotFound.Create(ADatabaseConnectionName);

    DatabaseConnectionConfiguration := TSSDatabaseConnectionConfiguration.Create(nil);

    DatabaseConnectionConfiguration.SetConnectionName(Q.GetDataSet.FieldByName('DB_CONNECTION_NAME').AsString);
    DatabaseConnectionConfiguration.SetConnectionString(Q.GetDataSet.FieldByName('CONNECTION_STRING').AsString);
    DatabaseConnectionConfiguration.SetDriverClassName(Q.GetDataSet.FieldByName('DRIVER_CLASS_NAME').AsString);
    DatabaseConnectionConfiguration.SetConnectionName(Q.GetDataSet.FieldByName('DB_CONNECTION_NAME').AsString);

    Result := DatabaseConnectionConfiguration;
  finally
    Q.ReleaseObjectInstance;
    Q := nil;
  end;
end;

end.
